﻿<cfsilent>
	<!--- set variable defaults --->
	<cfset variables.CGA = 15>
	<cfset variables.CGADisplay = "15%">
	<cfset variables.CR = 1><!--- default to 1 --->
	<cfset variables.ProgressionRate = 2><!--- default to Medium --->
	<cfset variables.TreasureType = 1><!--- default to Standard --->

	<!--- get values from form --->
	<cfif StructKeyExists(form,"CR")>
		<cfset variables.CR = form.CR>
	</cfif>
	<cfif StructKeyExists(form,"ProgressionRate")>
		<cfset variables.ProgressionRate = form.ProgressionRate>
	</cfif>
	<cfif StructKeyExists(form,"TreasureType")>
		<cfset variables.TreasureType = form.TreasureType>
	</cfif>

	<!--- get lookup values --->
	<cfquery name="variables.GetCRs" datasource="#application.DSN#">
		select min(CR) as minCR, max(CR) as maxCR
		from BaseTreasure
		order by CR
	</cfquery>

	<cfquery name="variables.GetProgressionRates" datasource="#application.DSN#">
		select ProgressionID, ProgressionName
		from ProgressionRates
	</cfquery>

	<cfquery name="variables.GetTreasureTypes" datasource="#application.DSN#">
		select TreasureTypeID, TreasureType
		from TreasureTypes
		order by Multiplier
	</cfquery>
</cfsilent><cfmodule template="_header.cfm">
	<!--- add data to header here  --->
	<title>Pathfinder Treasure Generator v.0.0.1</title>

	<style type="text/css">
		.buttonWrapper {
			clear: both;
			text-align: center;
			width: 100%;
		}

		#cr, #cgaDisplay {
			border: none;
		}

		.fieldWrapper {
			clear: both;
			float: left;
			margin: .3em 0;
		}

		.label {
		    float: left;
		    font-weight: bold;
		    margin-right: 0.5em;
		    text-align: right;
		    width: 137px;
		}

		#results div {
			margin: .5em 0;
		}

		#slider {
			float: right;
			width: 250px;
		}

		#treasureGenerator {
			width: 450px;
		}

		#results ul {
			list-style-type: none;
		}
		.category {
			font-weight: bold;
		}
		.item {
			padding-left: 3em;
		}
	</style>
</cfmodule>
<form action="" id="treasureGenerator" method="post">
	<div class="fieldWrapper test"><label class="label" for="cr">Challenge Rating:</label><input type="text" name="cr" id="cr" value="<cfoutput>#variables.CR#</cfoutput>" size="1" /><div id="crSlider"></div></div>
	<div class="fieldWrapper">
		<label for="progressionRate">Progression Rate:</label>
			<div id="progressionRate">
			<cfoutput query="variables.GetProgressionRates">
				<input type="radio" id="progressionRate_#variables.GetProgressionRates.ProgressionID#" name="progressionRate" value="#variables.GetProgressionRates.ProgressionID#"<cfif variables.ProgressionRate eq variables.GetProgressionRates.ProgressionID> checked="checked"</cfif> /><label for="progressionRate_#variables.GetProgressionRates.ProgressionID#">#variables.GetProgressionRates.ProgressionName#</label>
			</cfoutput>
			</div>
		<!---<select name="progressionRate" id="progressionRate">
			<cfoutput query="variables.GetProgressionRates">
				<option value="#variables.GetProgressionRates.ProgressionID#"<cfif variables.ProgressionRate eq variables.GetProgressionRates.ProgressionID> selected="selected"</cfif>>#variables.GetProgressionRates.ProgressionName#</option>
			</cfoutput>
		</select>--->
	</div>
	<div class="fieldWrapper">
		<label class="label" for="treasureType">Treasure Type:</label>
		<select name="treasureType" id="treasureType">
			<cfoutput query="variables.GetTreasureTypes">
				<option value="#variables.GetTreasureTypes.TreasureTypeID#"<cfif variables.TreasureType eq variables.GetTreasureTypes.TreasureTypeID> selected="selected"</cfif>>#variables.GetTreasureTypes.TreasureType#</option>
			</cfoutput>
		</select>
	</div>
	<div class="fieldWrapper test">
		<label class="label" for="cga">% Coins/Gems/Art:</label><input type="text" name="cgaDisplay" id="cgaDisplay" value="<cfoutput>#variables.CGADisplay#</cfoutput>" size="3" />
		<input type="hidden" name="cga" id="cga" value="<cfoutput>#variables.CGA#</cfoutput>" />
		<div id="cgaSlider"></div>
	</div>
	<div class="buttonWrapper">
		<button id="getTreasure">Get Treasure!</button>
	</div>
</form>
<div id="results"></div>
<cfmodule template="_footer.cfm">
	<script type="text/javascript">
		$(document).ready( function() {
			$('#getTreasure').button()
				.click( function(e) {
					e.preventDefault();

					<!--- post form to cfcs --->
					var $form = $('#treasureGenerator');
					var data = {
						method: 'getTreasure',
						cga: $form.find($('#cga')).val(),
						cr: $form.find($('#cr')).val(),
						progressionID: $form.find($(':radio[name="progressionRate"]:checked')).val(),
						treasureTypeID: $form.find($('#treasureType')).val(),
					};
					$.ajax({
						data: data,
						success: function(data){
							var data = eval("(" + data + ")");
console.log(data);
							var $destination = $('#results').prepend('<div class="ui-widget-content ui-corner-all"><ul></ul></div>');
							$destination = $destination.children(':first').find('ul');
							$destination.append('<li class="category">Total GPV: ' + data.TotalGPV + ' [' + data.MaxGPV + ']</li>');

							$.each(data, function(index, value){
								if (index != "TotalGPV" && index != "MaxGPV" && value.length != 0) {
									$destination.append('<li class="category">' + index + '</li>');
console.log(value);
									$.each(value, function(index, data){
console.log(data);
										if (data.NAME) {
											$destination.append('<li class="item">' + data.NAME + ' (' + data.PRICE + ' gp)</li>');
										}

										$.each(data, function(index, value){
											if (index != "NAME" && index != "PRICE") {
												$destination.append('<li class="item">' + index + ': ' + value + '</li>');
											}
										});
								});
							}

						});
<!---							var data = eval("(" + data + ")");
console.log(data);
							var $destination = $('#results').prepend('<div class="ui-widget-content ui-corner-all"><ul></ul></div>');
							$destination = $destination.children(':first').find('ul');
							$destination.append('<li class="category">Total GPV:' + data.TotalGPV + ' [' + data.MaxGPV + ']</li>');

							$.each(data, function(index, value){
								if (index != "TotalGPV" && index != "MaxGPV" && value.length != 0) {
									$destination.append('<li class="category">' + index + '</li>');
									$.each(value, function(index, value) {
										$destination.append('<li class="item">' + value.DATA + '</li>');
//										console.log(value.DATA);
									});
								}
							});
--->
						}
					});
				});

			$( "#crSlider" ).slider({
				range: 'min',
				value: <cfoutput>#variables.CR#</cfoutput>,
				min: <cfoutput>#variables.GetCRs.MinCR#</cfoutput>,
				max: <cfoutput>#variables.GetCRs.MaxCR#</cfoutput>,
				slide: function( event, ui ) {
					$( "#cr" ).val( ui.value );
				}
			});

			$('#cgaSlider').slider({
				range: 'min',
				value: <cfoutput>#variables.CGA#</cfoutput>,
				min: 1,
				max: 100,
				slide: function(event, ui) {
					$('#cgaDisplay').val(ui.value + '%');
					$('#cga').val(ui.value);
				}
			});

			$('#progressionRate').buttonset();
		});

		$.ajaxSetup({
			data: {
				returnformat: 'json'
			},
			type: 'post',
			url: 'cfcs/treasureGenerator.cfc'
		});
	</script>
</cfmodule>